import Source from '../lexer/source'

import {
  trieIt,
  trieMerge,
  trieSearch
} from '../lexer/trie'

function search (trie, word) {
  let lex = new Source(word)
  lex.step = lex.scan
  return trieSearch(trie, lex, lex.step())
}

describe('trie', () => {
  it('should be work', () => {
    let trie = [0]

    expect(() => {
      trieMerge(trieIt('Bidi_C', 5), trie)
      trieMerge(trieIt('Bidi_Control', 6), trie)
      trieMerge(trieIt('Bidi_C', 5), trie)
      trieMerge(trieIt('Bidi', 4), trie)
    }).to.not.throw()

    expect(search(trie, 'B')).to.equal(0)
    expect(search(trie, 'Bidi_C')).to.equal(5)
    expect(search(trie, 'Bidi_M')).to.equal(0)
    expect(search(trie, 'Bidi_Control')).to.equal(6)
    expect(search(trie, 'Bidi_Controls')).to.equal(6)

    expect(() => {
      trieMerge(trieIt('Bidi_C', 1), trie)
    }).to.throw()
  })

  it('EOF', () => {
    let trie = [0, 0, [5]]
    expect(search(trie, '\x00')).to.equal(5)
  })

  it('action', () => {
    let trie = [1]
    trieMerge(trieIt('5', s => 5), trie)
    trieMerge(trieIt('6', s => 6), trie)
    trieMerge(trieIt('56', s => 56), trie)
    expect(search(trie, '5')()).to.equal(5)
    expect(search(trie, '6')()).to.equal(6)
    expect(search(trie, '56')()).to.equal(56)
    expect(search(trie, '7')).to.equal(1)
  })
})
